<?php

    session_start();

    include("function-access.php");
    $conn = connectSSO();
    
    $data_array = array();
    $sqlNotifMsg = "SELECT a.*, b.id_user, b.username, b.pid, b.user_type FROM tb_inbox a
                    LEFT JOIN tb_user b
                    ON b.id_user = a.sender
                    WHERE a.receiver = '".$_SESSION["ses_idUser"]."' ";
    // echo $sqlNotifMsg;
    
    switch ($_GET["act"]) {
        case 1:
            $sqlNotifMsg .= " GROUP BY a.sender ORDER BY a.status ASC, a.date_receive DESC LIMIT 5"; 
            // echo $sqlNotifMsg;
            $queryNotifBaru = mysqli_query($conn, $sqlNotifMsg);
            while($baris_list_pesan = mysqli_fetch_array($queryNotifBaru)){
                $row_array["idMsg"] = $baris_list_pesan["id_inbox"];
                $row_array["sender"] = $baris_list_pesan["username"];
                $row_array["idUser"] = $baris_list_pesan["id_user"];
                $row_array["subjek"] = (strlen($baris_list_pesan["message"]) > 20) ? substr($baris_list_pesan["message"], 0, 20)."..." : $baris_list_pesan["message"];
                $row_array["url"] = $baris_list_pesan["url"];
                $row_array["gambar"] = pictURL($baris_list_pesan["pid"], $baris_list_pesan["user_type"], 3);
                $row_array["tgl"] = dateTime($baris_list_pesan["date_receive"]);
                $row_array["statusNotif"] = $baris_list_pesan["status"];
                array_push($data_array,$row_array);
            }
            break;
        case 2:
            $sqlNotifMsg .= " AND a.status = '0' GROUP BY a.sender";
            // echo $sqlNotifMsg;
            $queryNotifBaru = mysqli_query($conn, $sqlNotifMsg);
            $banyakNotifBaru = mysqli_num_rows($queryNotifBaru);
            
            $row_array["banyakPesan"] = $banyakNotifBaru;
            array_push($data_array,$row_array);
            break;
        case 3:
            $sqlNotifMsg .= " AND a.status = '0' GROUP BY a.sender ORDER BY a.date_receive";
            // echo $sqlNotifMsg;
            $queryNotifBaru = mysqli_query($conn, $sqlNotifMsg);
            while($baris_list_pesan = mysqli_fetch_array($queryNotifBaru)){
                $row_array["idMsg"] = $baris_list_pesan["id_inbox"];
                $row_array["sender"] = $baris_list_pesan["username"];
                $row_array["idUser"] = $baris_list_pesan["id_user"];
                $row_array["subjek"] = (strlen($baris_list_pesan["message"]) > 20) ? substr($baris_list_pesan["message"], 0, 20)."..." : $baris_list_pesan["message"];
                $row_array["url"] = $baris_list_pesan["url"];
                $row_array["gambar"] = pictURL($baris_list_pesan["pid"], $baris_list_pesan["user_type"], 3);
                $row_array["tgl"] = dateTime($baris_list_pesan["date_receive"]);
                $row_array["statusNotif"] = $baris_list_pesan["status"];
                array_push($data_array,$row_array);
            }
            break;
    }
    
    echo json_encode($data_array);
?>